package in.muton.edushare.components.alertpanel;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.calendar.api.CalendarInterface;
import in.muton.edushare.components.calendar.api.EventHelper;
import in.muton.edushare.components.messaging.api.ScheduleMessage;
import in.muton.edushare.pages.user.UserCalendar;
import in.muton.edushare.schedules.DeleteSchedule;
import in.muton.edushare.schedules.widgets.DateLabel;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.Date;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.DateComparision;
import com.edushare.common.vo.ScheduleNotifications;
import com.edushare.common.vo.Schedules;
import com.edushare.common.vo.Users;

public class SchAlert extends Panel {

	public SchAlert(String id, IModel<DateComparision> model) {
		super(id);
		if (model.getObject() instanceof Schedules) {
			isNotification = false;
			schedule = new CayenneModel<Schedules>((Schedules) model
					.getObject());
		} else if (model.getObject() instanceof ScheduleNotifications) {
			isNotification = true;
			notification = new CayenneModel<ScheduleNotifications>(
					(ScheduleNotifications) model.getObject());
			schedule = new CayenneModel<Schedules>(notification.getObject()
					.getSchedule());
		}

		add(new Label("title", schedule.getObject().getSchTitle()));
		add(new DateLabel("startdate", CalendarInterface.getNextOccurence(
				schedule.getObject(), new Date()), 0));

		add(new Link<Void>("details") {
			private static final long serialVersionUID = -4772573618803067684L;
			@Override
			public void onClick() {
				setResponsePage(UserCalendar.class);
			}
		});

		add(new Link<Void>("hide") {
			@Override
			public void onClick() {
				notification.getObject().setActionStatus(
						ScheduleNotifications.HIDDEN);
				EduShareSession.get().getSessionORMInterface()
						.commitAllChanges();
			}

			private static final long serialVersionUID = -4772573618803067684L;

			@Override
			public boolean isVisible() {
				if (isNotification)
					return true;
				return false;
			}
		});
		add(new Link<Void>("delete") {
			@Override
			public void onClick() {
				new DeleteSchedule(schedule).delete();
				new ScheduleMessage(schedule.getObject(),
						ScheduleMessage.EVT_CANCELLED).send();
				EduShareSession.get().getSessionORMInterface()
						.commitAllChanges();
			}

			@Override
			public boolean isVisible() {
				if (schedule.getObject().getStatus() == Schedules.EVENT_STATUS_CANCELLED)
					return false;
				Users u = EduShareSession.get().getLoggedInUser();
				return EventHelper.isEditable(schedule.getObject(), u);
			}

			private static final long serialVersionUID = -4772573618803837483L;
		});
	}

	@Override
	public boolean isVisible() {
		if (isNotification) {
			if (notification.getObject().getActionStatus() == ScheduleNotifications.HIDDEN
					|| notification.getObject().getSchedule().getStatus() == Schedules.EVENT_STATUS_CANCELLED)
				return false;
		} else {
			if (schedule.getObject().getStatus() == Schedules.EVENT_STATUS_CANCELLED)
				return false;
		}
		return true;
	}

	boolean isNotification = true;
	IModel<Schedules> schedule;
	IModel<ScheduleNotifications> notification;

	private static final long serialVersionUID = 123891629125206138L;
}
